<template>
	<view class="goods-list">
		<view v-for="(item,index) in goodsList" :key="index" @click="gotoGoodsDetail(item)">
			
			<view class="goods-item">
				<!--左边盒子-->
				<view class="goods-item-left">
					<image class="goods-img" :src="item.goods_small_logo"></image>
				</view>
				<!--右边盒子-->
				<view class="goods-item-right">
					<view class="goods-info">
						<view class="goods-title">{{item.goods_name}}</view>
						<view class="goods-price">￥{{item.goods_price | tofixed}}</view>
					</view>
					
				</view>
			</view>
			
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				queryObj:{
					//查询关键字
					query:'',
					//商品分类id
					cid:'',
					//页码值
					pagenum:1,
					//每页显示多少条数据
					pagesize:10
				},
				//保存商品列表数据
				goodsList:[],
				//总条数
				total:0,
				//节流阀
				isLoading:false
			};
		},
		onLoad(options) {
			this.queryObj.query = options.query || ''
			this.queryObj.cid = options.cid || ''
			console.log(this.queryObj)
			
			
			this.getGoodsList()
		},
		methods:{
			//获取商品列表
			async getGoodsList(){
				this.isLoading = true
				const res =  await uni.$http.get('/api/public/v1/goods/search',this.queryObj)
				this.isLoading = false
				if(res.statusCode != 200){
					return uni.showMsg()
				}
				
				
				this.goodsList = [...this.goodsList,...res.data.message.goods]
				this.total = res.data.message.total
			},
			//跳转到商品详情页
			gotoGoodsDetail(item){
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id
				})
			}
		},
		filters:{
			tofixed(num){
				return Number(num).toFixed(2)
			}
		},
		//上拉触底
		onReachBottom() {
			//如果在加载
			if(this.isLoading) return
			 
			 //如果已经加载完毕
			 if(this.queryObj.pagenum * this.queryObj.pagesize >= this.total){
				 return uni.showMsg("已经加载完毕")
			 }
			this.queryObj.pagenum++
			this.getGoodsList()
		},
		//下拉刷新
		onPullDownRefresh(){
			this.queryObj.pagenum = 1
			this.isLoading = false
			this.total = 0
			this.goodsList = []
			
			this.getGoodsList()
			uni.stopPullDownRefresh()
		}
	}
</script>

<style lang="scss">
.goods-list{
	padding: 5px 5px;
	.goods-item{
		display: flex;
		
		.goods-item-left{
			border-bottom: 1px solid #f0f0f0;
			margin-right: 5px;
			.goods-img{
				width: 100px;
				height: 100px;
				display: block;
			}
			
		}
		.goods-item-right{
			.goods-info{
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				
				.goods-title{
					font-size: 13px;
					
				}
				.goods-price{
					font-size: 16px;
					color: #c00000;
				}
			}
			
		}
	}
}
</style>
